package com.shanzhu.beadhouse.controller;

import cn.hutool.json.JSONUtil;
import com.shanzhu.beadhouse.entity.base.Result;
import com.shanzhu.beadhouse.entity.query.AddLogQuery;
import com.shanzhu.beadhouse.entity.query.DeleteServiceLogQuery;
import com.shanzhu.beadhouse.entity.query.PageServiceLogQuery;
import com.shanzhu.beadhouse.service.IServiceLogService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * Unit tests for ServeLogController.
 */
public class ServeLogControllerTest {

    @InjectMocks
    private ServeLogController serveLogController;

    @Mock
    private IServiceLogService mockServiceLogService;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    /**
     * Test case: addServiceLog returns success result.
     */
    @Test
    public void testAddServiceLog_Success() {
        // Arrange
        AddLogQuery query = new AddLogQuery();

        Result expectedResult = Result.success("Added successfully");
        when(mockServiceLogService.addServiceLog(query)).thenReturn(expectedResult);

        // Act
        Result result = serveLogController.addServiceLog(query, "mockToken");

        // Assert
        assertNotNull(result);
        assertEquals(expectedResult, result);
        verify(mockServiceLogService, times(1)).addServiceLog(query);
    }

    /**
     * Test case: addServiceLog throws exception.
     */
    @Test
    public void testAddServiceLog_Exception() {
        // Arrange
        AddLogQuery query = new AddLogQuery();
        when(mockServiceLogService.addServiceLog(query)).thenThrow(new RuntimeException("DB error"));

        // Act & Assert
        assertThrows(RuntimeException.class, () -> {
            serveLogController.addServiceLog(query, "mockToken");
        });
    }

    /**
     * Test case: pageServiceLog returns success result.
     */
    @Test
    public void testPageServiceLog_Success() {
        // Arrange
        PageServiceLogQuery query = new PageServiceLogQuery();
        Result expectedResult = Result.success("Page data");
        when(mockServiceLogService.pageServiceLog(query)).thenReturn(expectedResult);

        // Act
        Result result = serveLogController.pageServiceLog(query, "mockToken");

        // Assert
        assertNotNull(result);
        assertEquals(expectedResult, result);
        verify(mockServiceLogService, times(1)).pageServiceLog(query);
    }

    /**
     * Test case: pageServiceLog throws exception.
     */
    @Test
    public void testPageServiceLog_Exception() {
        // Arrange
        PageServiceLogQuery query = new PageServiceLogQuery();
        when(mockServiceLogService.pageServiceLog(query)).thenThrow(new RuntimeException("DB error"));

        // Act & Assert
        assertThrows(RuntimeException.class, () -> {
            serveLogController.pageServiceLog(query, "mockToken");
        });
    }

    /**
     * Test case: deleteServiceLog returns success result and logs the query.
     */
    @Test
    public void testDeleteServiceLog_Success() {
        // Arrange
        DeleteServiceLogQuery query = new DeleteServiceLogQuery();
        Result expectedResult = Result.success("Deleted successfully");
        when(mockServiceLogService.deleteServiceLog(query)).thenReturn(expectedResult);

        // Act
        Result result = serveLogController.deleteServiceLog(query, "mockToken");

        // Assert
        assertNotNull(result);
        assertEquals(expectedResult, result);
        verify(mockServiceLogService, times(1)).deleteServiceLog(query);
    }

    /**
     * Test case: deleteServiceLog throws exception.
     */
    @Test
    public void testDeleteServiceLog_Exception() {
        // Arrange
        DeleteServiceLogQuery query = new DeleteServiceLogQuery();
        when(mockServiceLogService.deleteServiceLog(query)).thenThrow(new RuntimeException("DB error"));

        // Act & Assert
        assertThrows(RuntimeException.class, () -> {
            serveLogController.deleteServiceLog(query, "mockToken");
        });
    }
}
